﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.XSSF.UserModel;
using Warehouse.Application.Dto.Input;
using Warehouse.Application.Dto.Output;
using Warehouse.Application.Services.Externallabl;
using Warehouse.Domain.Entities;
using Warehouse.Result;

namespace Warehouse.Api.Controllers
{
    /// <summary>
    /// 外部标签
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ExternallabelController : ControllerBase
    {
        private readonly IExternallabelModelAppService _externallabelModelAppService;

        public ExternallabelController(IExternallabelModelAppService externallabelModelAppService)
        {
            _externallabelModelAppService = externallabelModelAppService;
        }
        


        /// <summary>
        /// 创建外部标签
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> CreateExternallabel(CreateExternallabelInput input)
        {
            return _externallabelModelAppService.CreateExternallabel(input);
        }

        /// <summary>
        /// 获取外部标签
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiPaing<ExternallabelOutput> GetExternallabel([FromQuery]PageInput input)
        {
            return _externallabelModelAppService.GetExternallabel(input);
        }

        /// <summary>
        /// 获取一条外部标签
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<ExternallabelModel> GetModelExternallabel(long Id)
        {
            return _externallabelModelAppService.GetModelExternallabel(Id);
        }

        /// <summary>
        /// 删除外部标签
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> DeleteExternallabel(long Id)
        {
            return _externallabelModelAppService.DeleteExternallabel(Id);
        }

        /// <summary>
        /// 创建外部标签
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public ApiResult<int> UpdateExternallabel(UpdateExternallabelInput input)
        {
            return _externallabelModelAppService.UpdateExternallabel(input);
        }


        /// <summary>
        /// 外部标签导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Derive()
        {
            var book = new XSSFWorkbook();
            var a = book.CreateSheet("标题");
            var row = a.CreateRow(0);
            row.CreateCell(0).SetCellValue("外部标签编号");
            row.CreateCell(1).SetCellValue("名称");
            row.CreateCell(2).SetCellValue("供应商");
            row.CreateCell(3).SetCellValue("格式");
            row.CreateCell(4).SetCellValue("分隔符");
            row.CreateCell(5).SetCellValue("拆分节数");
            row.CreateCell(6).SetCellValue("是否启用");
            row.CreateCell(7).SetCellValue("备注");

            var index = 1;
            var bs = _externallabelModelAppService.GetExternallabel(new PageInput { PageIndex = 1, PageSize = 1000 });
            foreach (var s in bs.PageDatas)
            {
                var rows = a.CreateRow(index++);
                rows.CreateCell(0).SetCellValue(s.Id);
                rows.CreateCell(1).SetCellValue(s.ExternalLabelName);
                rows.CreateCell(2).SetCellValue(s.SupplierName);
                rows.CreateCell(3).SetCellValue(s.Format);
                rows.CreateCell(4).SetCellValue(s.Separator);
                rows.CreateCell(5).SetCellValue(s.Resolution);
                rows.CreateCell(6).SetCellValue(s.Start);
                row.CreateCell(7).SetCellValue(s.ExternalLabelDescribe);
            }

            byte[] sb = null;
            using (var ms = new MemoryStream())
            {
                book.Write(ms);
                sb = ms.ToArray();
            }
            return File(sb, System.Net.Mime.MediaTypeNames.Application.Octet, "外部标签打印.xlsx");

        }








    }
}
